package com.sskj.run.ui.activity;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseActivity;
import com.sskj.run.R;
import com.sskj.run.R2;
import com.sskj.run.presenter.RechargePresenter;

import butterknife.BindView;

@Route(path = RConfig.RUN_TRANSFER)//转出/转入
public class TransferActivity extends BaseActivity<RechargePresenter> {


    @Autowired
    String moneyType;
    @BindView(R2.id.etNum)
    EditText etNum;
    @BindView(R2.id.ll_recharge_type)
    LinearLayout llRechargeType;
    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.btn_recharge)
    Button btnRecharge;
    @BindView(R2.id.ivQRCode)
    ImageView ivQRCode;
    @BindView(R2.id.tvSave)
    TextView tvSave;
    @BindView(R2.id.tvCode)
    TextView tvCode;
    @BindView(R2.id.tvCopy)
    TextView tvCopy;
    @BindView(R2.id.tvDesc)
    TextView tvDesc;
    @BindView(R2.id.tvMoney)
    TextView tvMoney;
    @BindView(R2.id.tvType)
    TextView tvType;
    @Autowired
    boolean isIn = true;
    @Autowired
    String id;
    @Autowired
    String money;


    @Override
    protected int getLayoutId() {
        return R.layout.run_activity_transfer;
    }

    @Override
    public RechargePresenter getPresenter() {
        return new RechargePresenter();
    }

    @Override
    protected void initView() {

        ARouter.getInstance().inject(this);
        setTitle(isIn ? App.INSTANCE.getString(R.string.run_billRecordBean2) : App.INSTANCE.getString(R.string.run_billRecordBean1));
        tvType.setText(String.format(App.INSTANCE.getString(R.string.run_transferActivity3), isIn ? App.INSTANCE.getString(R.string.run_billRecordBean2) : App.INSTANCE.getString(R.string.run_billRecordBean1), moneyType.toUpperCase()));
        if (isIn) {
            mPresenter.getUsable("1", id);
        } else {
            tvMoney.setText(String.format(App.INSTANCE.getString(R.string.run_transferActivity4), money));
        }
        ClickUtil.click(btnRecharge, () -> {
            if (TextUtils.isEmpty(etNum.getText().toString().trim())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.run_transferActivity5));
                return;
            }
            if (TextUtils.isEmpty(etPwd.getText().toString().trim())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.run_transferActivity6));
                return;
            }
//            if (Double.valueOf(etNum.getText().toString().trim()) > Double.valueOf(money)) {
//                ToastUtil.showShort(getString(R.string.run_recharge2Activity6));
//                return;
//            }

            mPresenter.exchange(isIn ? "1" : "4", isIn ? "4" : "1", etNum.getText().toString(), etPwd.getText().toString(), id);

        });
    }

    public void rechargeSuccess(String rechargeUrl) {
        ARouter.getInstance().build(RConfig.RUN_RECHATRGE_WEBVIEW).withString(Constans.RECHARGE_URL, rechargeUrl).navigation();
    }

    public void rechargeSuccess() {
        finish();
    }


    public void updateUsable(String usable) {
        money = usable;
        tvMoney.setText(String.format(App.INSTANCE.getString(R.string.run_transferActivity4), money));

    }

    public void exchangeSuccess() {
        RxBus.getDefault().send(RxBusCode.REFRESH_RUN_ASSET);
        finish();
    }
}
